// ./services/captcha.service.js

const { client: redisClient } = require('../config/redis.config');
const captcha = require('svg-captcha');

const CAPTCHA_PREFIX = 'captcha:';
const CAPTCHA_TTL = 300; // 5 分钟有效期

/**
 * 生成验证码并存入 Redis
 */
exports.generateCaptcha = async () => {
    const cap = captcha.create({
        size: 4,
        noise: 2,
        color: true,
        background: '#cc9966',
    });

    // 确保 ID 唯一性
    const captchaId = Date.now().toString() + Math.random().toString(36).substring(2, 8);
    const key = CAPTCHA_PREFIX + captchaId;

    await redisClient.set(key, cap.text.toLowerCase(), {
        EX: CAPTCHA_TTL,
    });

    return { captchaId, img: cap.data };
};

/**
 * 校验验证码，并从 Redis 中删除
 */
exports.verifyCaptcha = async (id, text) => {
    const key = CAPTCHA_PREFIX + id;
    const correctText = await redisClient.get(key);

    // 无论是否匹配，都立即从 Redis 中删除
    await redisClient.del(key);

    if (!correctText || correctText.toLowerCase() !== text.toLowerCase()) {
        return false;
    }
    return true;
};
